﻿using StoryPlan.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace StoryPlan
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<UsersContext>());
            Database.SetInitializer(new MainDataInitializer());


            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }

    public class MainDataInitializer : DropCreateDatabaseAlways<MainDataContext>
    {
        protected override void Seed(MainDataContext context)
        {
            base.Seed(context);

            var story1 = context.Stories.Create();

            story1.CreatedDate = new DateTime(1843, 12, 19, 12, 0, 0);
            story1.Description = "A christmas tale of morality";
            story1.LastModifiedDate = new DateTime(2011, 12, 25, 10, 30, 0);
            story1.Subtitle = "A Ghost Story at Christmas";
            story1.Title = "A Christmas Carol";
            story1.UserId = 1;

            var story2 = context.Stories.Create();

            story2.CreatedDate = new DateTime(1837, 2, 1, 9, 30, 0);
            story2.Description = "A tale of a young orphan";
            story2.LastModifiedDate = new DateTime(1839, 4, 1, 10, 30, 0);
            story2.Subtitle = "The Parish Boy's Progress";
            story2.Title = "Oliver Twist";
            story2.UserId = 1;


            var charCat1 = new CharacterCategory
            {
                Name = "Main Character",
                Description = "A main character in the story",
                UserId = 1
            };
            var charCat2 = new CharacterCategory
            {
                Name = "Minor Character",
                Description = "A minor character in the story",
                UserId = 1
            };

            var char1 = new Character
            {
                Forename = "Oliver",
                Surname = "Twist",
                Abbreviation = "OT",
                Category = charCat1,
                DateOfBirth = new DateTime(1990, 10, 3),
                DateOfDeath = new DateTime(2040, 12, 7),
                Description = "The main character of the story",
                Gender = Gender.Male,
                UserId = 1
            };

            var char2 = new Character
            {
                Forename = "John",
                Surname = "Twist",
                Abbreviation = "JT",
                Category = charCat2,
                DateOfBirth = new DateTime(1994, 12, 1),
                DateOfDeath = new DateTime(2047, 1, 21),
                Description = "The main character's previously unknown brother",
                Gender = Gender.Male,
                UserId = 1
            };

            story2.Characters.Add(char1);
            story2.Characters.Add(char2);

            context.Stories.Add(story1);
            context.Stories.Add(story2);


            context.SaveChanges();
        }
    }
}